# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=pyside6
pkgbase=mingw-w64-${_realname}
pkgname=(${MINGW_PACKAGE_PREFIX}-shiboken6
         ${MINGW_PACKAGE_PREFIX}-${_realname}
         ${MINGW_PACKAGE_PREFIX}-${_realname}-tools)
pkgver=6.9.0
pkgrel=1
pkgdesc="Enables the use of Qt6 APIs in Python applications (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://doc.qt.io/qtforpython/"
msys2_references=(
  'archlinux: pyside6'
  'purl: pkg:pypi/pyside6'
)
license=("spdx:LGPL-2.1-or-later")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-clang"
             "${MINGW_PACKAGE_PREFIX}-clang-tools-extra"
             "${MINGW_PACKAGE_PREFIX}-libxslt"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python-sphinx"
             "${MINGW_PACKAGE_PREFIX}-python-packaging"
             "${MINGW_PACKAGE_PREFIX}-python-numpy"
             "${MINGW_PACKAGE_PREFIX}-qt6-3d"
             "${MINGW_PACKAGE_PREFIX}-qt6-activeqt"
             "${MINGW_PACKAGE_PREFIX}-qt6-base"
             "${MINGW_PACKAGE_PREFIX}-qt6-charts"
             "${MINGW_PACKAGE_PREFIX}-qt6-connectivity"
             "${MINGW_PACKAGE_PREFIX}-qt6-datavis3d"
             $([[ ${MSYSTEM} == MINGW64 ]] || echo "${MINGW_PACKAGE_PREFIX}-qt6-graphs")
             "${MINGW_PACKAGE_PREFIX}-qt6-httpserver"
             "${MINGW_PACKAGE_PREFIX}-qt6-location"
             "${MINGW_PACKAGE_PREFIX}-qt6-multimedia"
             "${MINGW_PACKAGE_PREFIX}-qt6-networkauth"
             $([[ ${MSYSTEM} == MINGW64 || ${CARCH} == aarch64 ]] || echo "${MINGW_PACKAGE_PREFIX}-qt6-pdf")
             "${MINGW_PACKAGE_PREFIX}-qt6-quick3d"
             "${MINGW_PACKAGE_PREFIX}-qt6-positioning"
             "${MINGW_PACKAGE_PREFIX}-qt6-remoteobjects"
             "${MINGW_PACKAGE_PREFIX}-qt6-scxml"
             "${MINGW_PACKAGE_PREFIX}-qt6-sensors"
             "${MINGW_PACKAGE_PREFIX}-qt6-serialbus"
             "${MINGW_PACKAGE_PREFIX}-qt6-serialport"
             "${MINGW_PACKAGE_PREFIX}-qt6-speech"
             "${MINGW_PACKAGE_PREFIX}-qt6-svg"
             "${MINGW_PACKAGE_PREFIX}-qt6-tools"
             "${MINGW_PACKAGE_PREFIX}-qt6-webchannel"
             "${MINGW_PACKAGE_PREFIX}-qt6-websockets"
             "${MINGW_PACKAGE_PREFIX}-vulkan-headers")
_pkgfn=pyside-setup-everywhere-src-${pkgver}
source=(https://download.qt.io/official_releases/QtForPython/pyside6/PySide6-${pkgver}-src/${_pkgfn}.tar.xz
        001-fix-building-on-mingw.patch
        002-fix-build-qtexampleicons.patch
        003-skip-remote.patch)
sha256sums=('315b73bb7570d5b9e6793a8d3fafd1d2dd7f43d35eebb01d2b554ea206aad88e'
            '5f71b618e0ad687e9282fa4b8c3815beaf69b78666516bfce57cb039822f4ac6'
            'ad762d906e5e736b87e8fe92efd42045fe51206e6b6845e37d298a15a53ccfa3'
            '8fd95fe629efc2e492c0a0351a7aec3716a79804ac3ffe5b9676b23d859c0d7c')

prepare() {
  cd "${srcdir}"/${_pkgfn}
  patch -p1 -i "${srcdir}"/001-fix-building-on-mingw.patch
  patch -p1 -i "${srcdir}"/002-fix-build-qtexampleicons.patch
  # https://github.com/msys2/MINGW-packages/pull/23888#issue-2974803968
  patch -p1 -i "${srcdir}"/003-skip-remote.patch
}

build() {
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  _pyver=$(${MINGW_PREFIX}/bin/python -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${extra_config[@]} \
    -DBUILD_TESTS=OFF \
    -DFORCE_LIMITED_API=OFF \
    -DNO_QT_TOOLS="yes" \
    -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    -DNUMPY_INCLUDE_DIR=${MINGW_PREFIX}/include/python${_pyver}/numpy/_core/include \
    -DSKIP_MODULES=DBus \
    ../${_pkgfn}

  PYTHONPATH="${srcdir}"/build-${MSYSTEM}/sources \
  ${MINGW_PREFIX}/bin/cmake --build .
}

package_shiboken6() {
  pkgdesc="Generates bindings for C++ libraries using CPython source code (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
           "${MINGW_PACKAGE_PREFIX}-clang-libs"
           "${MINGW_PACKAGE_PREFIX}-qt6-base"
           "${MINGW_PACKAGE_PREFIX}-libxml2"
           "${MINGW_PACKAGE_PREFIX}-libxslt")

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install build-${MSYSTEM}/sources/shiboken6

  install -Dm644 ${_pkgfn}/sources/shiboken6/COPYING \
    "$pkgdir"${MINGW_PREFIX}/share/licenses/shiboken6/LICENSE

  # Fix *.pc files
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pkgdir}"${MINGW_PREFIX}/lib/pkgconfig/shiboken6.pc

  # Fix *.cmake files
  for _f in "${pkgdir}"${MINGW_PREFIX}/lib/cmake/Shiboken6/*.cmake; do
    sed -e "s|${MINGW_PREFIX}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done

  # Install egg-info
  cd "${srcdir}"/${_pkgfn}
  ${MINGW_PREFIX}/bin/python setup.py egg_info --build-type=shiboken6
  ${MINGW_PREFIX}/bin/python setup.py egg_info --build-type=shiboken6-generator
  _pythonpath=`${MINGW_PREFIX}/bin/python -c "from sysconfig import get_path; print(get_path('platlib'))"`
  cp -r shiboken6*.egg-info "$pkgdir"/$(cygpath ${_pythonpath})
}

package_pyside6() {
  pkgdesc="Enables the use of Qt6 APIs in Python applications (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
           "${MINGW_PACKAGE_PREFIX}-python"
           "${MINGW_PACKAGE_PREFIX}-shiboken6"
           "${MINGW_PACKAGE_PREFIX}-qt6-base"
           "${MINGW_PACKAGE_PREFIX}-qt6-declarative")
  optdepends=("${MINGW_PACKAGE_PREFIX}-qt6-3d"
              "${MINGW_PACKAGE_PREFIX}-qt6-activeqt"
              "${MINGW_PACKAGE_PREFIX}-qt6-charts"
              "${MINGW_PACKAGE_PREFIX}-qt6-connectivity"
              "${MINGW_PACKAGE_PREFIX}-qt6-datavis3d"
              "${MINGW_PACKAGE_PREFIX}-qt6-graphs"
              "${MINGW_PACKAGE_PREFIX}-qt6-httpserver"
              "${MINGW_PACKAGE_PREFIX}-qt6-location"
              "${MINGW_PACKAGE_PREFIX}-qt6-multimedia"
              "${MINGW_PACKAGE_PREFIX}-qt6-networkauth"
              "${MINGW_PACKAGE_PREFIX}-qt6-pdf"
              "${MINGW_PACKAGE_PREFIX}-qt6-quick3d"
              "${MINGW_PACKAGE_PREFIX}-qt6-positioning"
              "${MINGW_PACKAGE_PREFIX}-qt6-remoteobjects"
              "${MINGW_PACKAGE_PREFIX}-qt6-scxml"
              "${MINGW_PACKAGE_PREFIX}-qt6-sensors"
              "${MINGW_PACKAGE_PREFIX}-qt6-serialbus"
              "${MINGW_PACKAGE_PREFIX}-qt6-serialport"
              "${MINGW_PACKAGE_PREFIX}-qt6-speech"
              "${MINGW_PACKAGE_PREFIX}-qt6-svg"
              "${MINGW_PACKAGE_PREFIX}-qt6-tools"
              "${MINGW_PACKAGE_PREFIX}-qt6-webchannel"
              "${MINGW_PACKAGE_PREFIX}-qt6-websockets")

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install build-${MSYSTEM}/sources/pyside6

  install -Dm644 ${_pkgfn}/sources/pyside6/COPYING \
    "$pkgdir"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE

  _pythonpath=`${MINGW_PREFIX}/bin/python -c "from sysconfig import get_path; print(get_path('platlib'))"`
  # Install egg-info
  cd "${srcdir}"/${_pkgfn}
  ${MINGW_PREFIX}/bin/python setup.py egg_info --build-type=pyside6
  cp -r PySide6.egg-info "$pkgdir"/$(cygpath ${_pythonpath})
  # Install pyi files
  install -Dm644 "${srcdir}"/build-${MSYSTEM}/sources/pyside6/PySide6/*.pyi -t "$pkgdir"/$(cygpath ${_pythonpath})/PySide6
}

package_pyside6-tools() {
  pkgdesc="Tools for PySide6 (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-python"
           "${MINGW_PACKAGE_PREFIX}-pyside6")

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install build-${MSYSTEM}/sources/pyside-tools
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
